﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StructureMap;
using System.IO;

namespace Smugmug.Tests.CaptureRequests
{
    class Program
    {
        static void Main(string[] args)
        {
            Bootstrapper.Execute();

            WebRequestCollection requests = new WebRequestCollection();
            string path = @"C:\temp\smugmug\requests.xml";

            IWebClient wc = new WebClient();
            IProtocol protocol = new RestProtocol();
            ILogger logger = new NLogLogger();

            SmugmugClient client = new SmugmugClient(wc, protocol, logger);

            foreach (var method in Methods)
            {
                requests.Add(method.Invoke(client));
            }

            protocol = new JsonProtocol();
            client = new SmugmugClient(wc, protocol, logger);

            foreach (var method in Methods)
            {
                requests.Add(method.Invoke(client));
            }

            File.WriteAllText(path, SerializationHelper.SerializeXml<WebRequestCollection>(requests), Encoding.UTF8);
        }

        public static IEnumerable<Func<SmugmugClient, WebRequest>> Methods
        {
            get
            {
                yield return Login_Anonymously_EmptyApiKey;
                yield return Login_Anonymously_InvalidApiKey;
                yield return Login_Anonymously_ValidApiKey;
                yield return Login_Anonymously_AllNulls;
                yield return Login_WithPassword_InValidUser;
                yield return Login_WithPassword_ValidUser;
                yield return Login_WithPassword_AllNulls;
                yield return Login_WithHash_IncorrectHash;
                yield return Login_WithHash_CorrectHash;
            }
        }

        #region Login

        #region Login Anonymously

        public static WebRequest Login_Anonymously_InvalidApiKey(SmugmugClient client)
        {
            return Request.CaptureRequest(delegate
            {
                client.LoginAnonymously(TestValues.InvalidApiKey, null, null, null);
            }, client, Request.StandardKeyWildcards, null);
        }

        public static WebRequest Login_Anonymously_EmptyApiKey(SmugmugClient client)
        {
            return Request.CaptureRequest(delegate
            {
                client.LoginAnonymously("", null, null, null);
            }, client, Request.StandardKeyWildcards, null);
        }

        public static WebRequest Login_Anonymously_ValidApiKey(SmugmugClient client)
        {
            var valueWildcards = new List<string>
            {
                "APIKey",
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginAnonymously(Config.ApiKey, null, null, null);
            }, client, Request.StandardKeyWildcards, valueWildcards);
        }

        public static WebRequest Login_Anonymously_AllNulls(SmugmugClient client)
        {
            var wildcards = new List<string>
            {
                "APIKey",
                "Callback",
                "Pretty",
                "Strict"
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginAnonymously(null, null, null, null);
            }, client, wildcards, wildcards);
        }


        #endregion

        #region Login With Password

        /// <summary>
        /// These values are hard coded to avoid having a real user's credentials in code
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static WebRequest Login_WithPassword_ValidUser(SmugmugClient client)
        {
            if (client.Protocol is RestProtocol)
            {
                return new WebRequest
                {
                    Method = "smugmug.login.withPassword",
                    Response = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rsp stat=\"ok\"><method>smugmug.login.withPassword</method><Login PasswordHash=\"" + TestValues.PasswordHash + "\" AccountType=\"Pro\" FileSizeLimit=\"25165824\" SmugVault=\"1\"><Session id=\"f2b70638bdf57aaf2feeadf014d2786b\"/><User id=\"" + TestValues.UserId + "\" NickName=\"" + TestValues.Nickname + "\" DisplayName=\"" + TestValues.DisplayName + "\"/></Login></rsp>",
                    Url = "https://api.smugmug.com/services/api/rest/1.2.2/?method=smugmug.login.withPassword&APIKey=*&EmailAddress=" + TestValues.UserName + "&Password=" + TestValues.Password + "&Callback!=*&Pretty!=*&Strict!=*"
                };
            }
            else if(client.Protocol is JsonProtocol)
            {
                return new WebRequest
                {
                    Method = "smugmug.login.withPassword",
                    Response = "{\"stat\":\"ok\",\"method\":\"smugmug.login.withPassword\",\"Login\":{\"Session\":{\"id\":\"f6af896a7b2326cd9a4e2b2f1995c95b\"},\"User\":{\"id\":" + TestValues.UserId + ",\"NickName\":\"" + TestValues.Nickname + "\",\"DisplayName\":\"" + TestValues.DisplayName + "\"},\"PasswordHash\":\"" + TestValues.PasswordHash + "\",\"AccountType\":\"Pro\",\"FileSizeLimit\":25165824,\"SmugVault\":true}}",
                    Url = "https://api.smugmug.com/services/api/json/1.2.2/?method=smugmug.login.withPassword&APIKey=*&EmailAddress=" + TestValues.UserName + "&Password=" + TestValues.Password + "&Callback!=*&Pretty!=*&Strict!=*"
                };
            }
            return new WebRequest();
        }

        public static WebRequest Login_WithPassword_InValidUser(SmugmugClient client)
        {
            var valueWildcards = new List<string>
            {
                "APIKey"
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginWithPassword(Config.ApiKey, TestValues.InvalidUserName, TestValues.InvalidPassword, null, null, null);
            }, client, Request.StandardKeyWildcards, valueWildcards);
        }

        public static WebRequest Login_WithPassword_AllNulls(SmugmugClient client)
        {
            var wildcards = new List<string>
            {
                "APIKey",
                "EmailAddress",
                "Password",
                "Callback",
                "Pretty",
                "Strict"
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginWithPassword(null, null, null, null, null, null);
            }, client, wildcards, wildcards);
        }

        #endregion

        #region Login With Hash

        public static WebRequest Login_WithHash_IncorrectHash(SmugmugClient client)
        {
            var valueWildcards = new List<string>
            {
                "APIKey",
                "UserID"
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginWithHash(Config.UserId.Value, TestValues.InvalidHash, Config.ApiKey, null, null, null);
            }, client, Request.StandardKeyWildcards, valueWildcards);
        }

        public static WebRequest Login_WithHash_CorrectHash(SmugmugClient client)
        {
            var valueWildcards = new List<string>
            {
                "APIKey",
                "UserID",
                "PasswordHash"
            };

            return Request.CaptureRequest(delegate
            {
                client.LoginWithHash(Config.UserId.Value, Config.PasswordHash, Config.ApiKey, null, null, null);
            }, client, Request.StandardKeyWildcards, valueWildcards);
        }

        #endregion

        #endregion


    }
}